import {defineStore} from 'pinia'
import {getArticles, getArticleDetail} from '@/api/index.js'
import {ElMessage} from "element-plus";

export const useArticleStore = defineStore('article', {
    state: () => ({
        articles: null,
        loading: false,
        error: null
    }),

    actions: {
        async fetchArticles() {
            this.loading = true
            this.error = null
            try {
                const res = await getArticles()
                this.articles = res.data.results || res.data // 根据实际返回结构调整
            } catch (err) {
                this.error = err
                console.error('文章获取失败:', err)
            } finally {
                this.loading = false
            }
        },

    }
})

export const useArticleDetailStore = defineStore('articleDetail', {
    state: () => ({
        articleDetail: null,
        loading: false,
        error: null
    }),

    actions: {
        async fetchArticleDetail(id, slug) {
            this.loading = true
            this.error = null
            try {
                const res = await getArticleDetail(id, slug)
                this.articleDetail = res.data
            } catch (err) {
                this.error = err
                ElMessage.error('获取文章详情失败:', err)
            } finally {
                this.loading = false
            }
        },

    }
})